<?php

/**
 * Ein Wrapper fuer die PEAR Klasse HTML_TreeMenu
 *
 *
 * ACHTUNG !!!
 * Zeile 432 von TreeMenu.js wurde entsprechend angepasst:
 * document.cookie = 'TreeMenuBranchStatus=' + cookieString.join(':') + ';path=/';
 *
 */

require_once(WSB_MODULES_PATH . 'cms/Url.php');

class WSB_Dhtml_PageTree
{

	const MODE_PAGE_EDIT = 0;
	const MODE_CONTAINER_EDIT = 1;

	/**
	 * @var integer
	 */
	private $_mode;

	/**
	 * @var integer
	 */
	private $_pageId = 0;

	/**
	 * @param integer $mode
	 */
	public function __construct($mode=null)
	{
		$this->_mode = isset($mode) ? $mode : self::MODE_PAGE_EDIT ;
	}

	/**
	 * @return void
	 */
	public function render()
	{
    	$treeMenu = $this->getMenu();
    	$treeMenu->printMenu();
	}

	/**
	 * @param integer $id
	 */
	public function setPageId($id)
	{
		$this->_pageId = intval($id);
	}

	/**
	 * @return HTML_TreeMenu_DHTML
	 */
	public function getMenu()
	{
		$node = WSB_Node_Page::getTree();
		/*
    	$icon = $this->_getPageIcon($node);
    	preg_match('#src="(.+?)"#', $icon, $matches);
    	$iconsrc = $matches[1];
    	*/
    	$menu  = new HTML_TreeMenu();
		$treenode = new HTML_TreeNode(
			array(
				'text' => $node->title,
				'link' => $this->_getLink($node->getId()),
				'icon' => $this->_getIcon($node),
				'expandedIcon' => 'folder-expanded.gif',
				//'expanded' => true,
				'cssClass' => ($node->getId() == $this->_pageId) ? 'treeMenuDefaultCurrent' : 'treeMenuDefault'
			)
		);
		$this->_walkThroughPageNode5($node, $treenode);
		$menu->addItem($treenode);
		return new HTML_TreeMenu_DHTML($menu,
			array(
				//'images' => '../WSB/library/PEAR/HTML/TreeMenu/imagesAlt',
				'images' => '../WSB/design/default/skin/default/icon/famfam',
				'defaultClass' => 'treeMenuDefault'
			)
		);
	}

	/**
	 * @param WSB_Node_Page $node
	 * @param HTML_TreeNode $treenode
	 */
	private function _walkThroughPageNode5(WSB_Node_Page $node, HTML_TreeNode $treenode)
	{
		foreach($node->getChildren() AS $child) {
			$childtreenode = &$treenode->addItem(
				new HTML_TreeNode(array(
					'text' => $child->title,
					'link' => $this->_getLink($child->getId()),
					'icon' => $this->_getIcon($child),
					'expandedIcon' => $this->_getIcon($child, true),
					'cssClass' => ($child->getId() == $this->_pageId) ? 'treeMenuDefaultCurrent' : 'treeMenuDefault'
				))
			);
			if($child->hasChildren()) {
				$this->_walkThroughPageNode5($child, $childtreenode);
			}
		}
	}

	/**
	 * @param integer $pageId
	 * @return string
	 */
	private function _getLink($pageId)
	{
		$link = '';
		switch($this->_mode) {
			case self::MODE_PAGE_EDIT:
				$link = Cms_Url::pageEditUrl($pageId);
				break;
			case self::MODE_CONTAINER_EDIT:
				$link = Cms_Url::containerEditUrl($pageId);
				break;
		}
		return $link;
	}

	/**
	 * @param WSB_Node_Page $node
	 * @param boolean $expanded
	 * @return string
	 */
	private function _getIcon(WSB_Node_Page $node, $expanded = false)
	{

		// $expanded not used

		// Inaktive Seite: Keine spezielles Icon, da Seite keine "Funktion" hat
		if(!empty($node->hidden)) {
			return 'page_white.png';
		}
		// Shortcut, Nicht im Menu, Fegroups
		if(!empty($node->shortcut) && !empty($node->nav_hide) && !empty($node->fegroups)) {
			return 'page_shortcut_navhide_fegroups.png';
		}
		// Shortcut, Nicht im Menu
		if(!empty($node->shortcut) && !empty($node->nav_hide)) {
			return 'page_shortcut_navhide.png';
		}
		// Shortcut, Fegroups
		if(!empty($node->shortcut) && !empty($node->fegroups)) {
			return 'page_shortcut_fegroups.png';
		}
		// Nicht im Menu, Fegroups
		if(!empty($node->nav_hide) && !empty($node->fegroups)) {
			return 'page_navhide_fegroups.png';
		}
		// Shortcut
		if(!empty($node->shortcut)) {
			return 'page_shortcut.png';
		}
		// Nicht im Menu
		if(!empty($node->nav_hide)) {
			return 'page_navhide.png';
		}
		// Zugriff definiert
		if(!empty($node->fegroups)) {
			return 'page_fegroups.png';
		}
		return 'page.png';
	}

}
